﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class TextCategoryBll:ITextCategoryBll
    {
        private ITextCategoryDal _ICD;

        public TextCategoryBll(ITextCategoryDal ICD)
        {
            _ICD = ICD;
        }
        public async Task<int> AddCategoryAsync(string Title, int ParentId)
        {
            return await _ICD.AddModelAsync(new TextCategory
            {
                title = Title,
                parentId = ParentId
            });
        }

        public async Task<int> DeleteCategoryAsync(int Id)
        {
            var data = await _ICD.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _ICD.RemoveModelAsync(data);
        }

        public async Task<int> EditCategoryAsync(int Id, string Title, int ParentId)
        {
            var data = await _ICD.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.title = Title;
            data.parentId = ParentId;
            data.UpdateTime = DateTime.Now;
            return await _ICD.EditModelAsync(data);
        }

        public async Task<IEnumerable<TextCategoryDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _ICD.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(c => new TextCategoryDtos
            {
                id = c.Id,
                title = c.title,
                parentId = c.parentId,
              
            }) : new List<TextCategoryDtos>();
        }

        public async Task<TextCategoryDtos> GetDataById(int Id)
        {
            var data = await _ICD.GetDataByIdAsync(Id);
            return data == null ? new TextCategoryDtos() : new TextCategoryDtos
            {
                id = Id,
                title = data.title,
                parentId = data.parentId,
               
            };
        }

        public async Task<IEnumerable<TextCategoryDtos>> GetDataByTitle(string Title, int DeleteId = 1)
        {
            var data = await _ICD.GetDataByChooseAsync(r => r.DeleteId == DeleteId && r.title.Contains(Title));
            return data == null ? new List<TextCategoryDtos>() :
                data.Select(c => new TextCategoryDtos
                {
                    id = c.Id,
                    title = c.title,
                    parentId = c.parentId,
                   
                });
        }
    }
}
